<?php
namespace app\command;

use app\beautiful\model\partner\BonusModel;
use app\beautiful\model\partner\PartnerBonusModel;
use app\beautiful\model\PartnerModel;
use app\beautiful\model\setting\ConsumeBonusModel;
use app\beautiful\model\setting\PartnerBonusSettingModel;
use app\beautiful\model\setting\PartnerTaskBonusSettingModel;
use app\common\model\MemberConsumeModel;
use app\common\model\MemberMoneyDetailModel;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class Memberconsume_end extends Command
{
    protected function configure()
    {
        $this->setName('Memberconsume_end')
            ->addArgument('name', Argument::OPTIONAL, "your name")
            ->addOption('city', null, Option::VALUE_REQUIRED, 'city name')
            ->setDescription('Say Hello');
    }

    protected function execute(Input $input, Output $output)
    {
        $h=date('H');
        $i=date('i');
        $name = trim($input->getArgument('name'));
        if(($h!='00' || $i!='20') && empty($name)){
            $output->writeln("暂不能执行！每天凌晨20分执行" );exit();
        }
        do{
            $s=[];
            $s['status']=2;
            $s['bonus_id']=0;
            $list=MemberConsumeModel::where($s)->field('COUNT(id) as num,sum(reward_money) as all_bonus_price,start_time,end_time,shop_id,type,edit_time')->group('shop_id,type')->select()->toArray();
            if(empty($list)){
                $output->writeln("暂无数据".var_export([$s],true) );exit();
            }
            foreach ($list as $v){
                $where=[];
                $where['shop_id']=$v['shop_id'];
                $where['type']=$v['type'];
                $where['start_time']=strtotime($v['start_time']);
                $where['end_time']=strtotime($v['end_time']);
                $reward_money=reward_money($where);
                $p_s=[];
                $p_s[]=['shop_id','=',$v['shop_id']];
                $p_s[]=['status','=',2];
                $p_s[]=['bonus_id','=',0];
                $p_s[]=['type','=',$v['type']];
                $p_s[]=['start_time','=',$v['start_time']];
                $p_s[]=['reward_money','>',0];
                $bonus_num=MemberConsumeModel::where($p_s)->count();

                $bonus_in=[];
                $bonus_in['shop_id']=$v['shop_id'];
                $bonus_in['time_type']=$v['type'];
                $bonus_in['start_time']=$v['start_time'];
                $bonus_in['end_time']=$v['end_time'];
                $bonus_in['receive_time']=$v['edit_time'];
                $bonus_in['bonus_pool']=$reward_money;
                $bonus_in['bonus_total_price']=$v['all_bonus_price'];
                $bonus_in['member_num']=$v['num'];
                $bonus_in['member_bonus_num']=$bonus_num;
                $bonus_in['add_time']=date('Y-m-d H:i:s');
                $bonus_id=ConsumeBonusModel::insertGetId($bonus_in);
                $u_s=[];
                $u_s[]=['shop_id','=',$v['shop_id']];
                $u_s[]=['status','=',2];
                $u_s[]=['bonus_id','=',0];
                $u_s[]=['type','=',$v['type']];
                $u_s[]=['start_time','=',$v['start_time']];
                MemberConsumeModel::up(['bonus_id'=>$bonus_id],$u_s);
            }
        }while(true);
    }
}